package com.lujianfei.redpacketstat.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.lujianfei.redpacketstat.R
import com.lujianfei.redpacketstat.utils.StatusBarUtils
import kotlinx.coroutines.*

abstract class BaseActivity<DB> : AppCompatActivity(), CoroutineScope by MainScope() {

    protected var dataBinding:DB ?= null

    abstract fun layoutId():Int

    abstract fun initData()

    abstract fun initView()

    abstract fun initEvent()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /** 沉浸式状态栏效果设置 */
        StatusBarUtils.with(this)?.setColor(resources.getColor(R.color.colorPrimary))?.init()
        dataBinding = DataBindingUtil.setContentView(this, layoutId())
        initView()
        initData()
        initEvent()
    }

    fun launchOnUI(block: suspend CoroutineScope.() -> Unit) {
        launch(Dispatchers.Main) {
            block()
        }
    }

    fun launchOnIO(block: suspend CoroutineScope.() -> Unit) {
        launch(Dispatchers.IO) {
            block()
        }
    }

    override fun onDestroy() {
        cancel()
        super.onDestroy()
    }
}